package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IUserInfoRedisService redisService;

    @Autowired
    private IStrategyStatisVOService strategyStatisVOService;

    //攻略详情
    @GetMapping("/detail")
    public Object  detail(String id){
        Strategy strategy = strategyService.get(id);
        strategyStatisVOService.increaseViewnum(id,1);
        return JsonResult.success(strategy);
    }

    //攻略分页
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        Page<Strategy> list = strategyService.query(qo);
        return JsonResult.success(list);
    }

    //攻略主题
    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }


    //评论分页
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    //添加评论
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, HttpServletRequest request){
        //使用spring框架提供的BeanUtils工具类，将user到strategyComment中相同的属性全都拷到strategyContent中
        //没有的手动设置 由于id也拷过来了，所以在保存前设置id为null，不然id为user的id
        String token = request.getHeader("token");
        UserInfo user = redisService.getUserByToken(token);
        BeanUtils.copyProperties(user,strategyComment);
        strategyComment.setUserId(user.getId());
        strategyCommentService.save(strategyComment);
        strategyStatisVOService.increaseReplynum(strategyComment.getStrategyId(),1);
        return JsonResult.success();
    }

    //评论点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, HttpServletRequest request){
        //用户必须登录才能点赞 当用户第一次点赞时，点赞数加1,小手变红 将用户id加入id点赞列表中
        //第二次点赞时，点赞数减1，小手变白，将用户id从id点赞列表中去除
        StrategyComment comment = strategyCommentService.get(cid);
        String token = request.getHeader("token");
        UserInfo user = redisService.getUserByToken(token);
        List<String> thumbuplist = comment.getThumbuplist();
        if(!thumbuplist.contains(user.getId())){
            comment.setThumbupnum(comment.getThumbupnum()+1);
            thumbuplist.add(user.getId());
        }else{
            comment.setThumbupnum(comment.getThumbupnum()-1);
            thumbuplist.remove(user.getId());
        }
        strategyCommentService.update(comment);
        return JsonResult.success();
    }


    //问题：上面两个方法都要从request对象获取user对象，太麻烦
    //优化 将user对象存进spring容器中，这样随时可以拿user对象 使用@UserParam注解，表明是获取登录用户
    //使用自定义解析器解析请求参数
    //否则使用spring框架默认参数解析器解析参数
    @RequireLogin
    @GetMapping("/info")
    public Object info(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }
    @RequireLogin
    @GetMapping("/info2")
    public Object info2(UserInfo userInfo){
        return JsonResult.success(userInfo);
    }


    //查询vo对象
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVOService.getStrategyStaticVO(sid));
    }


    //收藏操作
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo user){
        boolean ret=strategyStatisVOService.increaseFavornum(sid,user.getId());
        return JsonResult.success(ret);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object thumbup(String sid,@UserParam UserInfo user){
        boolean ret=strategyStatisVOService.increaseThunbupnum(sid,user.getId());
        return JsonResult.success(ret);
    }
}
